package fatclientnbp;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import Model.Trip;
import edu.chl.chalmers.fatclient.sb.Flight;
import AirLine.*;
import javax.swing.*;
import java.sql.*;


public class BookingPanel extends JPanel implements ActionListener{


	private JButton book;
        private JPanel tripPanel;
        private JPanel passengerPanel;
        private ArrayList<Person> persons;
        
        private JTextField[] pnames;
        private JTextField[] pnumbers;
        private JComboBox[] pseats;
        
        private ArrayList<Booking> bookings;
        
 
        private Trip trip;
        private int nrPassengers;
        
        private MainController controller;
        
	public BookingPanel(Trip trip, int nrPassengers,MainController controller)
	{
            this.controller = controller;
            pnames=new JTextField[nrPassengers];
            pnumbers=new JTextField[nrPassengers];
            pseats=new JComboBox[nrPassengers];
            
            
            
            this.trip=trip;
            this.nrPassengers=nrPassengers;
            book=new JButton("Book");
            book.addActionListener(this);
            tripPanel=new JPanel();
            passengerPanel=new JPanel();
            passengerPanel.setSize(new Dimension(700, 200));
            createTables();
            createPassengers();
            showTables();

            

        }
	
        
       
        
	public void createTables()
	{
            String[] columns={"Airline", "Dep Airport", "Dest Airport", "Dep Time", "Arr Time", "Seats", "Price"};
                    
                   // DefaultTableModel model=new DefaultTableModel(7, t.getFlights().size()+1);       
                    JTable table=new JTable(trip.getFlights().size()+1, 7);
                    //table.setAutoResizeMode();
                    
                    for(int k=0; k<columns.length; k++)
                    {
                        table.setValueAt(columns[k], 0, k);
                    }

                   // table.getColumn(3).setMinWidth(25);
                    //table.getColumn(4).setMinWidth(25);
                        
                    int i=0;
                    for(Flight f: trip.getFlights())
                    {
                        i++;
                        table.setValueAt(f.getAirline().getName(), i, 0);                      
                        table.setValueAt(f.getDepartureAirport(), i, 1);
                        table.setValueAt(f.getDestinationAirport(), i, 2);
                        table.setValueAt(f.getDepartureTime(), i, 3);
                        table.setValueAt(f.getArrivalTime(), i, 4);
                        table.setValueAt(f.getFreeSeats().size(), i, 5);
                        table.setValueAt(f.getPrice(), i, 6);
                        
                    }
                  // tableList.add(table);
                   JTextField field=new JTextField(trip.getDepartureAirport()+"->"+trip.getDestinationAirport()+"  Duration: "+new Time(System.currentTimeMillis()+trip.getDuration()));
                   tripPanel.add(table);
                   tripPanel.add(field);
                 
                   //buttonList.add(button);
       
	}
	
        private void createPassengers()
        {
                passengerPanel.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx=0;
                c.gridy=0;
            
            String[] seatList={"Window", "Isle", "Row"};
            for(int i=0; i<nrPassengers; i++)
            {
                c.gridx=0;
                //JPanel panel=new JPanel();
                JTextField pname=new JTextField(20);
                JTextField pnumber=new JTextField(12);
                
                
                JComboBox box=new JComboBox(seatList);
                
                passengerPanel.add(new JLabel("Passenger name: "),c);
                c.gridx++;
                passengerPanel.add(pname, c);
                c.gridx++;
                passengerPanel.add(new JLabel("SSN: "), c);
                c.gridx++;
                passengerPanel.add(pnumber, c);
                c.gridx++;
                c.gridx++;
                passengerPanel.add(box, c);
                c.gridx++;
                c.gridy++;
                
                pnames[i]=pname;
                pnumbers[i]=pnumber;
                pseats[i]=box;
//              .gridx++;

                //passengerPanel.add(panel);
            }
        }
        
        public void showTables()
        {
            
                this.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx=0;
                c.gridy=0;
            
            this.add(tripPanel, c);
            c.gridy++;
            c.gridy++;
            this.add(passengerPanel, c);
            c.gridy++;
            this.add(book, c);
        }
        
        public ArrayList<Booking> getBookings()
        {
            return bookings;
        }
        
	public void actionPerformed(ActionEvent e) 
	{
            
            persons=new ArrayList<Person>();
            bookings=new ArrayList<Booking>();
            for(int i=0; i<nrPassengers; i++)
            {
                Person p=new Person(pnames[i].getText(), pnumbers[i].getText());
                
                
                persons.add(p);
                
                if(p.getName().length()>0 && p.getSSN().length()>0)
                {
                for(Flight f: trip.getFlights())
                {
                    Set<Seat> seats=f.getFreeSeats();
                    
                    Seat seat=null;
                    for(Seat s: seats)
                    {
                        //System.out.println("typ av säte: "+s.getType());
                        //if(!s.isBooked() && s.getType()==pseats[i].getSelectedIndex())
                        if(!s.isBooked())
                        {

                            seat=s;
                            s.setBooked(true);
                            
                        }
                        if(seat!=null)                       
                            break;
                        
                    }
                    
                    if(seat!=null)
                    bookings.add(new Booking(p, f, seat));
                    else
                        System.out.println("No free seat for that type, booking not done");
                   }
                }
                                    
                    
                    
            }
            if (bookings.size() < nrPassengers) {
                JDialog dia = new JDialog();
                JTextArea txt = new JTextArea();
                txt.setAlignmentX(JTextArea.CENTER_ALIGNMENT);
		txt.setEditable(false);
                txt.setTabSize(5);
		txt.setBackground(Color.LIGHT_GRAY);
		txt.setForeground(Color.BLACK);
		dia.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);		
		dia.setLayout(new BorderLayout());
                dia.add(txt,BorderLayout.CENTER);
                dia.setSize(new Dimension( (int) (txt.getPreferredSize().getWidth() + 10), 
                                           (int) (txt.getPreferredSize().getHeight() + 5)));
         	dia.setResizable(false);
                txt.setText("Unable to book :\n\t* Fields not correct filled\n\t* Not seat of the chosen type available");
                dia.add(txt);
                dia.pack();
                dia.setVisible(true);
            }
            else {           
          
                controller.book(bookings);
            }
                //            System.out.println("mmm!!! "+e.getActionCommand());
//            String tripid=e.getActionCommand();
//            Trip t=buttonMap.get(tripid);
//            System.out.println("jaee"+t.getFlights());

            
            
	}
	

    
	
}
